| Conditions | 1 |
| Paths | > 20000 |
| Total Lines | 237 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 16 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | |||
| 21 | |||
| 22 | iSelectNextHelper = 0; |
||
| 23 | iFocusedNextHelper = 0; |
||
| 24 | oContentVisible; |
||
| 25 | oContentScrollable; |
||
| 26 | |||
| 27 | sItemSelector; |
||
| 28 | sItemSelectedSelector; |
||
| 29 | sItemCheckedSelector; |
||
| 30 | sItemFocusedSelector; |
||
| 31 | |||
| 32 | sLastUid = ''; |
||
| 33 | oCallbacks = {}; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param {koProperty} koList |
||
| 37 | * @param {koProperty} koSelectedItem |
||
| 38 | * @param {koProperty} koFocusedItem |
||
| 39 | * @param {string} sItemSelector |
||
| 40 | * @param {string} sItemSelectedSelector |
||
| 41 | * @param {string} sItemCheckedSelector |
||
| 42 | * @param {string} sItemFocusedSelector |
||
| 43 | */ |
||
| 44 | constructor(koList, koSelectedItem, koFocusedItem, |
||
| 45 | sItemSelector, sItemSelectedSelector, sItemCheckedSelector, sItemFocusedSelector) |
||
| 46 | { |
||
| 47 | this.list = koList; |
||
| 48 | |||
| 49 | this.listChecked = ko.computed(() => _.filter(this.list(), (item) => item.checked())).extend({rateLimit: 0}); |
||
| 50 | this.isListChecked = ko.computed(() => 0 < this.listChecked().length); |
||
| 51 | |||
| 52 | this.focusedItem = koFocusedItem || ko.observable(null); |
||
| 53 | this.selectedItem = koSelectedItem || ko.observable(null); |
||
| 54 | |||
| 55 | this.itemSelectedThrottle = _.debounce(_.bind(this.itemSelected, this), 300); |
||
| 56 | |||
| 57 | this.listChecked.subscribe((items) => { |
||
| 58 | if (0 < items.length) |
||
| 59 | { |
||
| 60 | if (null === this.selectedItem()) |
||
| 61 | { |
||
| 62 | if (this.selectedItem.valueHasMutated) |
||
| 63 | { |
||
| 64 | this.selectedItem.valueHasMutated(); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | else |
||
| 68 | { |
||
| 69 | this.selectedItem(null); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | else if (this.autoSelect() && this.focusedItem()) |
||
| 73 | { |
||
| 74 | this.selectedItem(this.focusedItem()); |
||
| 75 | } |
||
| 76 | }, this); |
||
| 77 | |||
| 78 | this.selectedItem.subscribe((item) => { |
||
| 79 | |||
| 80 | if (item) |
||
| 81 | { |
||
| 82 | if (this.isListChecked()) |
||
| 83 | { |
||
| 84 | _.each(this.listChecked(), (subItem) => { |
||
| 85 | subItem.checked(false); |
||
| 86 | }); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (this.selectedItemUseCallback) |
||
| 90 | { |
||
| 91 | this.itemSelectedThrottle(item); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | else if (this.selectedItemUseCallback) |
||
| 95 | { |
||
| 96 | this.itemSelected(null); |
||
| 97 | } |
||
| 98 | |||
| 99 | }, this); |
||
| 100 | |||
| 101 | this.selectedItem = this.selectedItem.extend({toggleSubscribeProperty: [this, 'selected']}); |
||
| 102 | this.focusedItem = this.focusedItem.extend({toggleSubscribeProperty: [null, 'focused']}); |
||
| 103 | |||
| 104 | this.sItemSelector = sItemSelector; |
||
| 105 | this.sItemSelectedSelector = sItemSelectedSelector; |
||
| 106 | this.sItemCheckedSelector = sItemCheckedSelector; |
||
| 107 | this.sItemFocusedSelector = sItemFocusedSelector; |
||
| 108 | |||
| 109 | this.focusedItem.subscribe((item) => { |
||
| 110 | if (item) |
||
| 111 | { |
||
| 112 | this.sLastUid = this.getItemUid(item); |
||
| 113 | } |
||
| 114 | }, this); |
||
| 115 | |||
| 116 | let |
||
| 117 | aCache = [], |
||
| 118 | aCheckedCache = [], |
||
| 119 | mFocused = null, |
||
| 120 | mSelected = null; |
||
| 121 | |||
| 122 | this.list.subscribe((items) => { |
||
| 123 | |||
| 124 | if (isArray(items)) |
||
| 125 | { |
||
| 126 | _.each(items, (item) => { |
||
| 127 | if (item) |
||
| 128 | { |
||
| 129 | const uid = this.getItemUid(item); |
||
| 130 | |||
| 131 | aCache.push(uid); |
||
| 132 | if (item.checked()) |
||
| 133 | { |
||
| 134 | aCheckedCache.push(uid); |
||
| 135 | } |
||
| 136 | if (null === mFocused && item.focused()) |
||
| 137 | { |
||
| 138 | mFocused = uid; |
||
| 139 | } |
||
| 140 | if (null === mSelected && item.selected()) |
||
| 141 | { |
||
| 142 | mSelected = uid; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | }); |
||
| 146 | } |
||
| 147 | }, this, 'beforeChange'); |
||
| 148 | |||
| 149 | this.list.subscribe((aItems) => { |
||
| 150 | |||
| 151 | let |
||
| 152 | temp = null, |
||
| 153 | getNext = false, |
||
| 154 | isNextFocused = mFocused, |
||
| 155 | isChecked = false, |
||
| 156 | isSelected = false, |
||
| 157 | len = 0; |
||
| 158 | |||
| 159 | const |
||
| 160 | uids = []; |
||
| 161 | |||
| 162 | this.selectedItemUseCallback = false; |
||
| 163 | |||
| 164 | this.focusedItem(null); |
||
| 165 | this.selectedItem(null); |
||
| 166 | |||
| 167 | if (isArray(aItems)) |
||
| 168 | { |
||
| 169 | len = aCheckedCache.length; |
||
| 170 | |||
| 171 | _.each(aItems, (item) => { |
||
| 172 | |||
| 173 | const uid = this.getItemUid(item); |
||
| 174 | uids.push(uid); |
||
| 175 | |||
| 176 | if (null !== mFocused && mFocused === uid) |
||
| 177 | { |
||
| 178 | this.focusedItem(item); |
||
| 179 | mFocused = null; |
||
| 180 | } |
||
| 181 | |||
| 182 | if (0 < len && -1 < inArray(uid, aCheckedCache)) |
||
| 183 | { |
||
| 184 | isChecked = true; |
||
| 185 | item.checked(true); |
||
| 186 | len -= 1; |
||
| 187 | } |
||
| 188 | |||
| 189 | if (!isChecked && null !== mSelected && mSelected === uid) |
||
| 190 | { |
||
| 191 | isSelected = true; |
||
| 192 | this.selectedItem(item); |
||
| 193 | mSelected = null; |
||
| 194 | } |
||
| 195 | }); |
||
| 196 | |||
| 197 | this.selectedItemUseCallback = true; |
||
| 198 | |||
| 199 | if (!isChecked && !isSelected && this.autoSelect()) |
||
| 200 | { |
||
| 201 | if (this.focusedItem()) |
||
| 202 | { |
||
| 203 | this.selectedItem(this.focusedItem()); |
||
| 204 | } |
||
| 205 | else if (0 < aItems.length) |
||
| 206 | { |
||
| 207 | if (null !== isNextFocused) |
||
| 208 | { |
||
| 209 | getNext = false; |
||
| 210 | isNextFocused = _.find(aCache, (sUid) => { |
||
| 211 | if (getNext && -1 < inArray(sUid, uids)) |
||
| 212 | { |
||
| 213 | return sUid; |
||
| 214 | } |
||
| 215 | else if (isNextFocused === sUid) |
||
| 216 | { |
||
| 217 | getNext = true; |
||
| 218 | } |
||
| 219 | return false; |
||
| 220 | }); |
||
| 221 | |||
| 222 | if (isNextFocused) |
||
| 223 | { |
||
| 224 | temp = _.find(aItems, (oItem) => isNextFocused === this.getItemUid(oItem)); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | this.selectedItem(temp || null); |
||
| 229 | this.focusedItem(this.selectedItem()); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | if ((0 !== this.iSelectNextHelper || 0 !== this.iFocusedNextHelper) && 0 < aItems.length && !this.focusedItem()) |
||
| 234 | { |
||
| 235 | temp = null; |
||
| 236 | if (0 !== this.iFocusedNextHelper) |
||
| 237 | { |
||
| 238 | temp = aItems[-1 === this.iFocusedNextHelper ? aItems.length - 1 : 0] || null; |
||
| 239 | } |
||
| 240 | |||
| 241 | if (!temp && 0 !== this.iSelectNextHelper) |
||
| 242 | { |
||
| 243 | temp = aItems[-1 === this.iSelectNextHelper ? aItems.length - 1 : 0] || null; |
||
| 244 | } |
||
| 245 | |||
| 246 | if (temp) |
||
| 247 | { |
||
| 248 | if (0 !== this.iSelectNextHelper) |
||
| 249 | { |
||
| 250 | this.selectedItem(temp || null); |
||
| 251 | } |
||
| 252 | |||
| 253 | this.focusedItem(temp || null); |
||
| 254 | |||
| 255 | this.scrollToFocused(); |
||
| 256 | |||
| 257 | _.delay(() => this.scrollToFocused(), 100); |
||
| 258 | } |
||
| 752 |